package com.phonemap.models;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.google.appengine.api.datastore.Blob;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.phonemap.common.Util;

public class Photo {

	public static Entity createOrUpdatePhoto(long userId, String title,
			String caption, String contentType, byte[] photoData, String photoPath) {
		Entity user = User.getUser(userId);
		Entity photo = new Entity("Photo", user.getKey());
		photo.setProperty("userId", userId);
		photo.setProperty("title", title);
		photo.setProperty("caption", caption);
		photo.setProperty("contentType", contentType);
		photo.setProperty("photoData", new Blob(photoData));
		photo.setProperty("photoPath", photoPath);
		photo.setProperty("photoDate", new Date());

		Util.persistEntity(photo);
		//update avatar to user
		user.setProperty("avatar", photo.getKey().getId());
		Util.persistEntity(user);
		return photo;
	}

//	public static Entity getSinglePhoto(long userId) {
//		Iterable<Entity> results = Util.listEntities("Photo", "user", Long.toString(userId));
//		List<Entity> entity = new ArrayList<Entity>();
//		for (Entity e : results)
//			if (e != null)
//				entity.add(e);
//		if (!entity.isEmpty()) {
//			return (Entity) entity.remove(0);
//		}
//		return null;
//	}

	public static Iterable<Entity> getAllPhotos() {
		Iterable<Entity> entities = Util.listEntities("Photo", null, null);
		return entities;
	}
	
	public static Iterable<Entity> getAllPhotos(String kind) {
		Iterable<Entity> entities = Util.listEntities(kind, null, null);
		return entities;
	}

	public static Iterable<Entity> getPhoto(long userId) {
		Iterable<Entity> entities = Util.listEntities("Photo", "user", Long.toString(userId));
		return entities;
	}
	
	public static Entity findPhotoById(long id, long userId) {
		Key parentKey = KeyFactory.createKey("User", userId);
		Key key = KeyFactory.createKey(parentKey, "Photo", id);
		Entity entity = Util.findEntity(key);
		return entity;
	}

	public static Iterable<Entity> getRelatedPhonePosstions(String phone) {
		Iterable<Entity> userList = User.getAllUsers();
		List<Entity> photoList = new ArrayList<Entity>();
		for (Entity user : userList) {
			Entity e = Util.getMaxEntities("Photo", "userId", user
					.getProperty("phone").toString(), "photoDate");
			if (e != null) {
				if (phone.equals(e.getProperty("userId").toString())) {
					if (photoList.size() > 0) {
						photoList.add(photoList.get(0));
						photoList.set(0, e);
					} else {
						photoList.add(0, e);
					}
				} else {
					photoList.add(e);
				}

			}
		}
		return photoList;
	}

//	public static String deletePhoto(String photoKey) {
//		Entity entity = getSinglePhoto(photoKey);
//		if (entity != null) {
//			Util.deleteEntity(entity.getKey());
//			return ("Photo deleted successfully.");
//		} else
//			return ("Photo not found");
//	}
	
	/**
	   * Get all the photos for a user
	   * 
	   * @param kind
	   *          : photo kind
	   * @param userName
	   *          : user name
	   * @return: all photos of type user
	   */
	  public static Iterable<Entity> getPhotosForUser(String kind, String userName) {
	    Key ancestorKey = KeyFactory.createKey("User", userName);
	    return Util.listChildren("Photo", ancestorKey);
	  }
}
